﻿//============================================================================
// BDInfo - Blu-ray Video and Audio Analysis Tool
// Copyright © 2009 Cinema Squid
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BD2MKV
{
    public partial class FormSettings : Form
    {
        public FormSettings()
        {
            InitializeComponent();

            checkBoxAutosaveReport.Checked = BDMuxSettings.AutosaveReport;
            checkBoxGenerateStreamDiagnostics.Checked = BDMuxSettings.GenerateStreamDiagnostics;
            checkBoxGenerateTextSummary.Checked = BDMuxSettings.GenerateTextSummary;
            checkBoxFilterLoopingPlaylists.Checked = BDMuxSettings.FilterLoopingPlaylists;
            checkBoxFilterShortPlaylists.Checked = BDMuxSettings.FilterShortPlaylists;
            textBoxFilterShortPlaylistsValue.Text = BDMuxSettings.FilterShortPlaylistsValue.ToString();
            checkBoxUseImagePrefix.Checked = BDMuxSettings.UseImagePrefix;
            textBoxUseImagePrefixValue.Text = BDMuxSettings.UseImagePrefixValue;
            checkBoxKeepStreamOrder.Checked = BDMuxSettings.KeepStreamOrder;
            checkBoxDecodeDTS.Checked = BDMuxSettings.DecodeDTS;
            textBoxEac3to.Text = BDMuxSettings.eac3toLocation;
            textBoxBDSup2Sub.Text = BDMuxSettings.BDSup2SubLocation;
            textBoxMkvmerge.Text = BDMuxSettings.mkvmergeLocation;
            textBoxX264.Text = BDMuxSettings.x264Location;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            BDMuxSettings.AutosaveReport = checkBoxAutosaveReport.Checked;
            BDMuxSettings.GenerateStreamDiagnostics = checkBoxGenerateStreamDiagnostics.Checked;
            BDMuxSettings.GenerateTextSummary = checkBoxGenerateTextSummary.Checked;
            BDMuxSettings.KeepStreamOrder = checkBoxKeepStreamOrder.Checked;
            BDMuxSettings.UseImagePrefix = checkBoxUseImagePrefix.Checked;
            BDMuxSettings.UseImagePrefixValue = textBoxUseImagePrefixValue.Text;
            BDMuxSettings.FilterLoopingPlaylists = checkBoxFilterLoopingPlaylists.Checked;
            BDMuxSettings.FilterShortPlaylists = checkBoxFilterShortPlaylists.Checked;
            BDMuxSettings.DecodeDTS = checkBoxDecodeDTS.Checked;
            BDMuxSettings.eac3toLocation = textBoxEac3to.Text;
            BDMuxSettings.BDSup2SubLocation = textBoxBDSup2Sub.Text;
            BDMuxSettings.mkvmergeLocation = textBoxMkvmerge.Text;
            BDMuxSettings.x264Location = textBoxX264.Text;

            int filterShortPlaylistsValue;
            if (int.TryParse(textBoxFilterShortPlaylistsValue.Text, out filterShortPlaylistsValue))
            {
                BDMuxSettings.FilterShortPlaylistsValue = filterShortPlaylistsValue;
            }
            BDMuxSettings.SaveSettings();
            Close();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormSettings_Load(object sender, EventArgs e)
        {

        }

        private string BrowseWindow(string title)
        {
            string path = null;
            try
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                dialog.Description = title;
                dialog.ShowNewFolderButton = false;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    path = dialog.SelectedPath;                    
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format(
                    "Error opening path {0}: {1}{2}",
                    path,
                    ex.Message,
                    Environment.NewLine);

                MessageBox.Show(msg, "BDMux Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return path;
        }

        private void buttonEac3to_Click(object sender, EventArgs e)
        {
            textBoxEac3to.Text = BrowseWindow("Select the eac3to Folder:");            
        }

        private void buttonBDSup2Sub_Click(object sender, EventArgs e)
        {
            textBoxBDSup2Sub.Text = BrowseWindow("Select the BDSup2Sub Folder:");            
        }

        private void buttonMkvmerge_Click(object sender, EventArgs e)
        {
            textBoxMkvmerge.Text = BrowseWindow("Select the mkvmerge Folder:");           
        }

        private void buttonX264_Click(object sender, EventArgs e)
        {
            textBoxX264.Text = BrowseWindow("Select the x264 Folder:");
        }
    }

    public class BDMuxSettings
    {
        public static bool GenerateStreamDiagnostics
        {
            get
            {
                try { return Properties.Settings.Default.GenerateStreamDiagnostics; }
                catch { return true; }
            }

            set
            {
                try { Properties.Settings.Default.GenerateStreamDiagnostics = value; }
                catch { }
            }
        }

        public static bool AutosaveReport
        {
            get
            {
                try { return Properties.Settings.Default.AutosaveReport; }
                catch { return false; }
            }

            set
            {
                try { Properties.Settings.Default.AutosaveReport = value; }
                catch { }
            }
        }

        public static bool GenerateFrameDataFile
        {
            get
            {
                try { return Properties.Settings.Default.GenerateFrameDataFile; }
                catch { return false; }
            }

            set
            {
                try { Properties.Settings.Default.GenerateFrameDataFile = value; }
                catch { }
            }
        }

        public static bool FilterLoopingPlaylists
        {
            get
            {
                try { return Properties.Settings.Default.FilterLoopingPlaylists; }
                catch { return false; }
            }

            set
            {
                try { Properties.Settings.Default.FilterLoopingPlaylists = value; }
                catch { }
            }
        }

        public static bool FilterShortPlaylists
        {
            get
            {
                try { return Properties.Settings.Default.FilterShortPlaylists; }
                catch { return false; }
            }

            set
            {
                try { Properties.Settings.Default.FilterShortPlaylists = value; }
                catch { }
            }
        }

        public static int FilterShortPlaylistsValue
        {
            get
            {
                try { return Properties.Settings.Default.FilterShortPlaylistsValue; }
                catch { return 0; }
            }

            set
            {
                try { Properties.Settings.Default.FilterShortPlaylistsValue = value; }
                catch { }
            }
        }

        public static bool UseImagePrefix
        {
            get
            {
                try { return Properties.Settings.Default.UseImagePrefix; }
                catch { return false; }
            }

            set
            {
                try { Properties.Settings.Default.UseImagePrefix = value; }
                catch { }
            }
        }

        public static string UseImagePrefixValue
        {
            get
            {
                try { return Properties.Settings.Default.UseImagePrefixValue; }
                catch { return null; }
            }

            set
            {
                try { Properties.Settings.Default.UseImagePrefixValue = value; }
                catch { }
            }
        }

        public static bool KeepStreamOrder
        {
            get
            {
                try { return Properties.Settings.Default.KeepStreamOrder; }
                catch { return false; }
            }

            set
            {
                try { Properties.Settings.Default.KeepStreamOrder = value; }
                catch { }
            }
        }

        public static bool GenerateTextSummary
        {
            get
            {
                try { return Properties.Settings.Default.GenerateTextSummary; }
                catch { return false; }
            }

            set
            {
                try { Properties.Settings.Default.GenerateTextSummary = value; }
                catch { }
            }
        }

        public static string LastPath
        {
            get
            {
                try { return Properties.Settings.Default.LastPath; }
                catch { return ""; }
            }

            set
            {
                try { Properties.Settings.Default.LastPath = value; }
                catch { }
            }
        }

        public static string LastFilePath
        {
            get
            {
                try { return Properties.Settings.Default.LastFilePath; }
                catch { return ""; }
            }

            set
            {
                try { Properties.Settings.Default.LastFilePath = value; }
                catch { }
            }
        }

        public static string LastTarget
        {
            get
            {
                try { return Properties.Settings.Default.LastTarget; }
                catch { return ""; }
            }

            set
            {
                try { Properties.Settings.Default.LastTarget = value; }
                catch { }
            }
        }

        public static bool RemoveTemp
        {
            get
            {
                try { return Properties.Settings.Default.RemoveTemp; }
                catch { return true; }
            }

            set
            {
                try { Properties.Settings.Default.RemoveTemp = value; }
                catch { }
            }
        }

        public static bool FTPTargets
        {
            get
            {
                try { return Properties.Settings.Default.FTPTargets; }
                catch { return true; }
            }

            set
            {
                try { Properties.Settings.Default.FTPTargets = value; }
                catch { }
            }
        }

        public static int Schema
        {
            get
            {
                try { return Properties.Settings.Default.Schema; }
                catch { return 0; }
            }

            set
            {
                try { Properties.Settings.Default.Schema = value; }
                catch { }
            }
        }

        public static string eac3toLocation
        {
            get
            {
                try { return Properties.Settings.Default.eac3toLocation; }
                catch { return ""; }
            }

            set
            {
                try { Properties.Settings.Default.eac3toLocation = value; }
                catch { }
            }
        }

        public static string BDSup2SubLocation
        {
            get
            {
                try { return Properties.Settings.Default.BDSup2SubLocation; }
                catch { return ""; }
            }

            set
            {
                try { Properties.Settings.Default.BDSup2SubLocation = value; }
                catch { }
            }
        }

        public static string mkvmergeLocation
        {
            get
            {
                try { return Properties.Settings.Default.mkvmergeLocation; }
                catch { return ""; }
            }

            set
            {
                try { Properties.Settings.Default.mkvmergeLocation = value; }
                catch { }
            }
        }

        public static string x264Location
        {
            get
            {
                try { return Properties.Settings.Default.x264Location; }
                catch { return ""; }
            }

            set
            {
                try { Properties.Settings.Default.x264Location = value; }
                catch { }
            }
        }

        public static string FTPServer
        {
            get
            {
                try { return Properties.Settings.Default.FTPServer; }
                catch { return ""; }
            }

            set
            {
                try { Properties.Settings.Default.FTPServer = value; }
                catch { }
            }
        }

        public static string FTPDir
        {
            get
            {
                try { return Properties.Settings.Default.FTPDir; }
                catch { return ""; }
            }

            set
            {
                try { Properties.Settings.Default.FTPDir = value; }
                catch { }
            }
        }

        public static string FTPUser
        {
            get
            {
                try { return Properties.Settings.Default.FTPUser; }
                catch { return ""; }
            }

            set
            {
                try { Properties.Settings.Default.FTPUser = value; }
                catch { }
            }
        }

        public static string FTPPWD
        {
            get
            {
                try { return Properties.Settings.Default.FTPPWD; }
                catch { return ""; }
            }

            set
            {
                try { Properties.Settings.Default.FTPPWD = value; }
                catch { }
            }
        }

        public static bool DecodeDTS
        {
            get
            {
                try { return Properties.Settings.Default.DecodeDTS; }
                catch { return false; }
            }

            set
            {
                try { Properties.Settings.Default.DecodeDTS = value; }
                catch { }
            }
        }

        public static void SaveSettings()
        {
            try
            {
                Properties.Settings.Default.Save();
            }
            catch { }
        }

    }
}
